package dao;

import java.sql.*;
import java.util.*;

/**
 * UserRank: tum0r
 * Package Name: dao
 * Time: 2019-01-13 Sunday 14:31:01
 */

public class SQL {

    String sqlDriver = "com.mysql.jdbc.Driver";

    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet resultSet = null;

    String encode = "utf-8";

    public boolean open(String host, String port, String databaseName, String userName, String password) {
        return open(host, port, databaseName, userName, password, this.encode);
    }

    public boolean open(String host, String port, String databaseName, String userName, String password, String encode) {
        try {
            Class.forName(sqlDriver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        StringBuilder url = new StringBuilder();
        url.append("jdbc:mysql://");
        url.append(host);
        url.append(":");
        url.append(port);
        url.append("/");
        if (databaseName != null) {
            url.append(databaseName);
        }
        url.append("?useUnicode=true&useSSL=false&allowPublicKeyRetrieval=true&characterEncoding=");
        url.append(encode);
        url.append("&user=");
        url.append(userName);
        url.append("&password=");
        url.append(password);
        try {
            connection = DriverManager.getConnection(url.toString());
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean close() {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    private void setArgs(String command, Object[] args) {
        try {
            statement = connection.prepareStatement(command);
            if (args != null) {
                for (int i = 0; i < args.length; i++) {
                    statement.setObject(i + 1, args[i]);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public LinkedList<LinkedHashMap<String, Object>> query(String command, Object[] args) {
        try {
            LinkedList<LinkedHashMap<String, Object>> result = new LinkedList();
            LinkedHashMap<String, Object> temp;
            setArgs(command, args);
            int count = 0;
            resultSet = statement.executeQuery();
            count = resultSet.getMetaData().getColumnCount();
            while (resultSet.next()) {
                temp = new LinkedHashMap();
                for (int i = 0; i < count; i++) {
                    temp.put(resultSet.getMetaData().getColumnName(i+1), resultSet.getObject(i+1));
                }
                result.add(temp);
            }
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public int update(String command, Object[] args) {
        try {
            setArgs(command, args);
            return statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}